//
//  CDLogoViewController.m
//  CollectToDesktop
//
//  Created by 王启颖 on 2020/10/16.
//  Copyright © 2020 godbe. All rights reserved.
//

#import "CDLogoViewController.h"
#import "CDLogoCell.h"
#import "CDWallpaperViewController.h"
@interface CDLogoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) NSMutableArray *bgArr;
@end

@implementation CDLogoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self tableView];
}

- (void)setBgID:(NSNumber *)bgID{
    _bgID = bgID;
    [self request];
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kDeviceWidth, kDeviceHeight)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = UIColor.whiteColor;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
//        [_tableView registerClass:[CDLogoCell class] forCellReuseIdentifier:@"logoCell"];
        [self.tableView registerNib:[UINib nibWithNibName:@"CDLogoCell" bundle:nil] forCellReuseIdentifier:@"logoCell"];
    }
    return _tableView;
}

#pragma mark - UITableViewDelegate -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.bgArr.count;
    
}
   
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CDLogoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"logoCell"];
    NSDictionary *dic = self.bgArr[indexPath.row];
    [cell.bgImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",dic[@"wallpaperUrl"]]] placeholderImage:[UIImage imageNamed:@"1234"]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    WEAKSELF
    cell.checkClickBlock = ^{
        CDWallpaperViewController *vc =[[CDWallpaperViewController alloc]init];
        vc.dic = dic;
        [weakSelf.navigationController pushViewController:vc animated:YES];
        
    };
    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{




}
-(NSMutableArray *)bgArr{
    if (!_bgArr) {
        _bgArr = [[NSMutableArray alloc]init];
    }
    return _bgArr;
}
#pragma mark - 网络请求
-(void)request{

    NSString *urlstr=[NSString stringWithFormat:@"https://gapi.szlzyd.com/0010//na/icon/wallpaper"];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    dic[@"wallpaperClassificationId"] = self.bgID;
    [[CDNetworkRequset HttpRequstManager] PostURL:urlstr params:dic success:^(NSInteger code, id  _Nonnull result) {

        
        self.bgArr = [NSMutableArray arrayWithArray:result[@"data"]];
        [self.tableView reloadData];
        
    } fail:^(NSError * _Nonnull error) {
        
    }];
}


@end
